

/**
 * 地形裁剪
 */

import Polygon from "../../entity/type/Polygon"
import Transform from "../../transform/Transform"
import TOP_IMG from '../images/excavate_kuangqu.jpg'
import BOTTOM_IMG from '../images/excavate_bottom_min.jpg'

export default class TerrainClipping {

  /**
   * 
   * @param {*} viewer 
   * @param {*} positions 
   * @param {*} distance 
   */
  constructor(viewer, options = {}) {
    this._viewer = viewer
    this._positions = options.positions
    this._distance = options.distance || 10
    this._image = options.image || TOP_IMG
    this._imageBottom = options.imageBottom || BOTTOM_IMG
    this._globe = this._viewer.scene.globe

    this._overlayLayer = new Cesium.CustomDataSource('Terrain-clipping-overlay-layer')
    this._viewer.dataSources.add(this._overlayLayer)

    this.start()
  }


  start(){
    let clippingPlanes = this.getClippingPlanes()
    this._globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
      planes: clippingPlanes,
      // edgeWidth: 1.0,
      // edgeColor: Cesium.Color.YELLOW
    })

    this.createBottom()
    this.createTop()
  }
  /**
   * 获取多边形裁剪平面
   * @returns 
   */
  getClippingPlanes() {
    let pointsLength = this._positions.length;
    let positions = this.degreesToWGS84()
    let clippingPlanes = []; // 存储ClippingPlane集合
    for (var i = 0; i < pointsLength; ++i) {
      let points = positions
      let nextIndex = (i + 1) % pointsLength
      let midpoint = Cesium.Cartesian3.add(points[i], points[nextIndex], new Cesium.Cartesian3())
      midpoint = Cesium.Cartesian3.multiplyByScalar(midpoint, 0.5, midpoint)
      let up = Cesium.Cartesian3.normalize(midpoint, new Cesium.Cartesian3())
      let right = Cesium.Cartesian3.subtract(points[nextIndex], midpoint, new Cesium.Cartesian3())
      right = Cesium.Cartesian3.normalize(right, right)

      let normal = Cesium.Cartesian3.cross(right, up, new Cesium.Cartesian3())
      normal = Cesium.Cartesian3.normalize(normal, normal)

      clippingPlanes.push(new Cesium.ClippingPlane(normal, this._distance))
    }
    return clippingPlanes
  }

  /**
   * 
   */
  createBottom(){
    let bottomPositions = Transform.DegreesArrayToCartesianArray(this._positions , this._distance) 
    bottomPositions = new Cesium.Cartesian3.fromDegreesArrayHeights(bottomPositions)

    let overlay = new Polygon(bottomPositions , {
      closeTop: false, // 这个要设置为false
      perPositionHeight: true, // 这个要设置true
      material: new Cesium.ImageMaterialProperty({
        image: this._imageBottom
      }),
    })
    this._overlayLayer.entities.add(overlay.delegate)
    this._viewer.zoomTo(overlay.delegate)
    return overlay
  }

  /**
   * 
   */
  createTop(){
    let topPositions = Transform.DegreesArrayToCartesianArray(this._positions) 
    topPositions = new Cesium.Cartesian3.fromDegreesArray(topPositions)

    let overlay = new Polygon(topPositions , {
      closeTop: false, // 这个要设置为false
      closeBottom : false,
      extrudedHeight: -this._distance * 2,
      material: new Cesium.ImageMaterialProperty({
          image: this._image
      }),
      perPositionHeight: true 
    })
    this._overlayLayer.entities.add(overlay.delegate)
    return overlay
  }

  /**
   * 
   */
  destroy(){

  }

  degreesToWGS84(){
    let positions = []
    for (let index = 0; index < this._positions.length; index++) {
      const element = this._positions[index];
      let cartesian3 = new Cesium.Cartesian3.fromDegrees(element[0],element[1],element[2] || 0)
      positions.push(cartesian3)
    }
    return positions
  }

}